/*!
 * @file beep.c
 * @brief 蜂鸣器控制
 *
 * <pre>
 * 通过GPIO控制蜂鸣器的开关
 *
 * </pre>
 * @author ssjhs
 * @date 2024-07-30
 *
 */

/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include "main.h"

#include "tx_api.h"
#include "shell.h"

/* External variables ---------------------------------------------------------*/

/* Private includes ----------------------------------------------------------*/

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/
// static TX_EVENT_FLAGS_GROUP tx_events;
// static TX_THREAD tx_task;
// static uint8_t tx_task_memory[TASK_STACK_SIZE];
static TX_TIMER tx_timer;

/* Private function prototypes -----------------------------------------------*/
// static void task_entry(ULONG arg);
static int beep(int);
static void tx_timer_function(ULONG id);

/* Function prototypes -----------------------------------------------*/
static void tx_timer_function(ULONG id)
{
    UNUSED(id);
    HAL_GPIO_WritePin(GPIOG, GPIO_PIN_12, GPIO_PIN_RESET); // deactivate
}

static int beep(int ms)
{
    int timeout = ms / 10;
    if (timeout == 0)
    {
        HAL_GPIO_WritePin(GPIOG, GPIO_PIN_12, GPIO_PIN_RESET); // activate
        return 0;
    }
    HAL_GPIO_WritePin(GPIOG, GPIO_PIN_12, GPIO_PIN_SET); // activate

    if (tx_timer.tx_timer_id != 0)
    {
        tx_timer_delete(&tx_timer);
    }

    tx_timer_create(&tx_timer, "beep_timer", tx_timer_function, 0, timeout, 0, TX_AUTO_ACTIVATE);

    return 0;
}

/****************** letter shell register *********************************/
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC), beep, beep, millisecond);
